package Ldap_app::Controller::Utils;
use Mojo::Base 'Mojolicious::Controller';
use Email::Valid;
use Try::Tiny;
use Crypt::SaltedHash;

# This action will render a template
sub confirm {
    my $self = shift;
    my $token = $self->param('token') || '';
    if ( $token =~ /^\s*$/ ) {
        return $self->render(
                template => 'utils/notice',
                token => $token,
                msg    => "token is empty!",
        );

    }

    my $expct=0;
    my ( $mail, $time ) ;
    try {
        ( $mail, $time ) = split( ":", $self->decrypt($token) );
    }
    catch {
        $expct=1;
    };

    if ($expct){
         return $self->render(
                template => 'utils/notice',
                token => $token,
                msg    => "token is illegal!",
        );


    }

    if ( not Email::Valid->address($mail) and $time =~ m{^\d+$} ) {
        return $self->render(
                template => 'utils/notice',
                token => $token,
                msg    => "token is illegal!",
        );

    }

    if ( $self->expired($time) ) {
        return $self->render(
                template => 'utils/notice',
                token => $token,
                msg    => "token is expired!",
        );
    }

    my $mesg = $self->search_mail($mail);
    my $max = $mesg->count;

    if ( $max != 1 ) {
        return $self->render(
                template => 'utils/notice',
                token => $token,
                msg    => "account $mail find  $max entry !",
        );
    }

    $self->render(
                token => $token,
                msg  => "Please input your new password",
    );

}

# This action will render a template
sub flush {
    my $self = shift;
    my $token = $self->param('token') || '';
    my $pwd = $self->param('pw') || '';
    if ( $token =~ /^\s*$/ ) {
        return $self->render(
                msg    => "token is empty!",
        );

    }

    my ( $mail, $time ) = split( ":", $self->decrypt($token) );
    if ( not Email::Valid->address($mail) and $time =~ m{^\d+$} ) {
        return $self->render(
                msg    => "token is illegal!",
        );

    }

    if ( $self->expired($time) ) {
        return $self->render(
                msg    => "token is expired!",
        );
    }

    my $mesg = $self->search_mail($mail);
    my $max = $mesg->count;
    if ( $max != 1 ) {
        return $self->render(
                msg    => "account $mail find  $max entry !",
        );
    }

    my $entry = $mesg->entry(0);
    my $shash = Crypt::SaltedHash->new(algorithm => 'SHA-1');
    $shash->add($pwd);
    my $salted = $shash->generate;
    my $ldap=$self->ldap;
     $entry->replace(
        userPassword => $salted,
    );

    my $res=$entry->update( $ldap ) ;
    my $code = $res->code;
   my $msg=$res->error;
    if ($code != 0){
    return $self->render(
            msg    => "reset password failure: $code $msg",
    );

 
    }

    $self->render(
            msg    => "reset password done",
    );

}

sub apply {
    my $self = shift;
    my $mail = $self->param('mail') || '';

    unless ( Email::Valid->address($mail) ) {
        return $self->render(
                msg    => "email is illegal!",
        );

    }

    my $mesg = $self->search_mail($mail);
    my $max = $mesg->count;
    if (  $max == 0  ) {
        return $self->render(
                 msg    => "email $mail is not find in database !",
        );
    }


    my $token = $self->encrypt($mail);
    my $url   = Mojo::URL->new( $self->config->{server} );
    $url->path('/action/update');
    $url->query( token => $token );
    my $link = $url;
    try {
        $self->mail(
            to      => $mail,
            subject => "apply reset account password is confirm",
            data =>
"Dear colleague,</br>please click this link to reset you account password!</br> <a href=\"$link\">reset password</a> or copy this link to your browser </br> $link </br> ",
            type     => 'text/html',
            nomailer => 1,
            headers  => [ { 'X-My-Header' => 'sample ldap app by mojo' } ],

        );
    }
    catch {
        $self->app->log->info("send alter mail fail! $!");
    };

    return $self->render(
            msg    => "confirm email had send to your inbox , please check it",
    );

}

1;
